//定义全局变量 读取api 接口地址
const apiPath = '/';

// 获取高度
function getHeight(value) {
	var num = 88;
	var Height = uni.getSystemInfoSync().statusBarHeight; //状态栏的高度
	if (value) {
		num = value;
	}
	return Number(Height * 2 + num) + 'upx'
}

// 提示方法
function showDialog(msg, icon) {
	var newIncon = 'none';
	if (icon) {
		newIncon = icon;
	}
	return uni.showToast({
		title: msg,
		icon: newIncon
	})
};

// 优惠券状态名
function couponState(state, source) {
	switch (state) {
		case 1:
			if (source == 'detail') {
				return '立即使用';
			} else {
				return '去使用';
			}
			break;
		case 2:
			return '已使用';
			break;
		case 3:
			return '已失效';
			break;
		default:
			return '立即领取';
			break;
	}
}

//获取商品主图
function getMainImage(images) {
	var imgUrl = images[0].path;
	images.forEach((item, index) => {
		if (item.is_main == 1) {
			imgUrl = item.path;
		}
	})
	return imgUrl;
};

//商品图片列表重排
function getImages(images) {
	console.log('images',images);
	var imgIndex = -1;
	var img = {};
	images.forEach((item, index) => {
		if (item.is_main == 1) {
			img = item;
			imgIndex = index;
		}
	})
	if (imgIndex > 0) {
		images.splice(imgIndex, 1);
		images.unshift(img);
	}
	return images;
};

//选择规格后的商品价格
function spec(specId, DATA, type) {
	specId = specId.toString();
	var goods_spec_price = DATA.goods_spec_price
	if(type == 'point') {
		goods_spec_price = DATA.activites_spec_price
	}
	for (var i = 0; i < goods_spec_price.length; i++) {
		//选择后的商品规格ID组合对比商品组合ID，匹对返回商品组合价格
		if (specId == goods_spec_price[i].unid) {
			return goods_spec_price[i]
		}
	}
};

module.exports = {
	getHeight: getHeight,
	showDialog: showDialog,
	couponState: couponState,
	getMainImage: getMainImage,
	getImages: getImages,
	spec: spec,
}
